SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[stateMail]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[stateMail](
	[stateId] [varchar](10) NOT NULL,
	[type] [varchar](255) NOT NULL,
	[description] [varchar](512) NULL,
 CONSTRAINT [PK_stateMail] PRIMARY KEY CLUSTERED 
(
	[stateId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[userData]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[userData](
	[userId] [varchar](10) NOT NULL,
	[name] [varchar](255) NULL,
	[apellido] [varchar](30) NULL,
	[telefono] [varchar](10) NULL,
	[direccion] [varchar](255) NULL,
	[sexo] [varchar](10) NULL,
 CONSTRAINT [PK_userData] PRIMARY KEY CLUSTERED 
(
	[userId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_importanceMailSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_importanceMailSelect] 
    @importanceId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [importanceId], [description], [type] 
	FROM   [dbo].[importanceMail] 
	WHERE  ([importanceId] = @importanceId OR @importanceId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_importanceMailInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_importanceMailInsert] 
    @importanceId char(10),
    @description char(512),
    @type char(255)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[importanceMail] ([importanceId], [description], [type])
	SELECT @importanceId, @description, @type
	
	-- Begin Return Select <- do not remove
	SELECT [importanceId], [description], [type]
	FROM   [dbo].[importanceMail]
	WHERE  [importanceId] = @importanceId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_importanceMailUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_importanceMailUpdate] 
    @importanceId char(10),
    @description char(512),
    @type char(255)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[importanceMail]
	SET    [importanceId] = @importanceId, [description] = @description, [type] = @type
	WHERE  [importanceId] = @importanceId
	
	-- Begin Return Select <- do not remove
	SELECT [importanceId], [description], [type]
	FROM   [dbo].[importanceMail]
	WHERE  [importanceId] = @importanceId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_importanceMailDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_importanceMailDelete] 
    @importanceId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[importanceMail]
	WHERE  [importanceId] = @importanceId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_mensajeSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'

CREATE PROC [dbo].[usp_mensajeSelect] 
    @mensajeID CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [mensajeID], [attach], [content], [createDate], [head], [importance], [subject] 
	FROM   [dbo].[mensaje] 
	WHERE  ([mensajeID] = @mensajeID OR @mensajeID IS NULL) 

	COMMIT


' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_mensajeInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_mensajeInsert] 
    @mensajeID char(10),
    @attach binary(10),
    @content binary(10),
    @createDate datetime,
    @head char(512),
    @importanceId char(10),
    @subject char(255)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[mensaje] ([mensajeID], [attach], [content], [createDate], [head], [importanceId], [subject])
	SELECT @mensajeID, @attach, @content, @createDate, @head, @importanceId, @subject
	
	-- Begin Return Select <- do not remove
	SELECT [mensajeID], [attach], [content], [createDate], [head], [importanceId], [subject]
	FROM   [dbo].[mensaje]
	WHERE  [mensajeID] = @mensajeID
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_mensajeUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_mensajeUpdate] 
    @mensajeID char(10),
    @attach binary(10),
    @content binary(10),
    @createDate datetime,
    @head char(512),
    @importanceId char(10),
    @subject char(255)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[mensaje]
	SET    [mensajeID] = @mensajeID, [attach] = @attach, [content] = @content, [createDate] = @createDate, [head] = @head, [importanceId] = @importanceId, [subject] = @subject
	WHERE  [mensajeID] = @mensajeID
	
	-- Begin Return Select <- do not remove
	SELECT [mensajeID], [attach], [content], [createDate], [head], [importanceId], [subject]
	FROM   [dbo].[mensaje]
	WHERE  [mensajeID] = @mensajeID	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_mensajeDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_mensajeDelete] 
    @mensajeID char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[mensaje]
	WHERE  [mensajeID] = @mensajeID

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[importanceMail]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[importanceMail](
	[importanceId] [varchar](255) NOT NULL,
	[type] [varchar](255) NOT NULL,
	[description] [varchar](512) NULL,
 CONSTRAINT [PK_importanceMail] PRIMARY KEY CLUSTERED 
(
	[importanceId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mensaje]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[mensaje](
	[mensajeID] [varchar](10) NOT NULL,
	[subject] [varchar](255) NULL,
	[importance] [varchar](255) NOT NULL,
	[head] [varchar](512) NULL,
	[content] [binary](512) NULL,
	[attach] [binary](10) NULL,
	[createDate] [datetime] NOT NULL,
 CONSTRAINT [PK_mensaje] PRIMARY KEY CLUSTERED 
(
	[mensajeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[office]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[office](
	[officeId] [varchar](10) NOT NULL,
	[name] [varchar](255) NULL,
	[description] [varchar](512) NULL,
	[createDate] [datetime] NULL,
	[updateDate] [datetime] NULL,
	[relationshipId] [varchar](10) NULL,
 CONSTRAINT [PK_office] PRIMARY KEY CLUSTERED 
(
	[officeId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[LogType]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[LogType](
	[type] [varchar](10) NOT NULL,
	[description] [varchar](512) NULL,
 CONSTRAINT [PK_LogType] PRIMARY KEY CLUSTERED 
(
	[type] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Mail]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Mail](
	[MailId] [varchar](10) NOT NULL,
	[mensajeId] [varchar](10) NOT NULL,
	[CC] [bit] NOT NULL,
	[reciveDate] [datetime] NOT NULL,
	[sendDate] [datetime] NOT NULL,
	[stateId] [varchar](10) NOT NULL,
	[createDate] [datetime] NOT NULL,
	[updateDate] [datetime] NULL,
 CONSTRAINT [PK_Mail_1] PRIMARY KEY CLUSTERED 
(
	[MailId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[casillaCorreo]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[casillaCorreo](
	[casillaCorreoId] [varchar](10) NOT NULL,
	[userId] [varchar](10) NOT NULL,
	[description] [varchar](512) NULL,
	[createDate] [datetime] NULL,
	[updateDate] [datetime] NULL,
	[password] [varchar](255) NOT NULL,
 CONSTRAINT [PK_casillaCorreo] PRIMARY KEY CLUSTERED 
(
	[casillaCorreoId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MailEnviados]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MailEnviados](
	[casillaCorreoEnviado] [varchar](10) NOT NULL,
	[casillaCorreoRecibido] [varchar](10) NOT NULL,
	[mailId] [varchar](10) NOT NULL,
	[mailEnviadosId] [varchar](10) NOT NULL,
 CONSTRAINT [PK_MailEnviados] PRIMARY KEY CLUSTERED 
(
	[mailEnviadosId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[correoOffice]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[correoOffice](
	[officeId] [varchar](10) NOT NULL,
	[casillaCorreoId] [varchar](10) NOT NULL,
 CONSTRAINT [PK_correoOffice] PRIMARY KEY CLUSTERED 
(
	[officeId] ASC,
	[casillaCorreoId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Sessiones]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Sessiones](
	[sessionId] [varchar](512) NOT NULL,
	[casillaCorreoId] [varchar](10) NULL,
	[officeId] [varchar](10) NULL,
	[description] [varchar](512) NULL,
	[createDate] [datetime] NOT NULL,
 CONSTRAINT [PK_Sessiones] PRIMARY KEY CLUSTERED 
(
	[sessionId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[listaCorreo]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[listaCorreo](
	[casillaCorreoId] [varchar](10) NOT NULL,
	[id] [varchar](10) NOT NULL,
	[name] [varchar](255) NOT NULL,
	[description] [varchar](512) NULL,
	[createDate] [datetime] NOT NULL,
	[updateDate] [datetime] NULL,
 CONSTRAINT [PK_listaCorreo] PRIMARY KEY CLUSTERED 
(
	[casillaCorreoId] ASC,
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Log]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Log](
	[date] [datetime] NOT NULL,
	[type] [varchar](10) NOT NULL,
	[description] [varchar](512) NOT NULL,
	[casillaCorreoId] [varchar](10) NOT NULL,
 CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED 
(
	[date] ASC,
	[type] ASC,
	[description] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[relationship]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[relationship](
	[relationshipId] [varchar](10) NOT NULL,
	[officeId] [varchar](10) NOT NULL,
	[description] [varchar](512) NULL,
	[createDate] [datetime] NOT NULL,
	[updateDate] [datetime] NULL,
 CONSTRAINT [PK_relationship] PRIMARY KEY CLUSTERED 
(
	[relationshipId] ASC,
	[officeId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_stateMailSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_stateMailSelect] 
    @stateId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [stateId], [description], [type] 
	FROM   [dbo].[stateMail] 
	WHERE  ([stateId] = @stateId OR @stateId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_stateMailInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_stateMailInsert] 
    @stateId char(10),
    @description char(512),
    @type char(255)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[stateMail] ([stateId], [description], [type])
	SELECT @stateId, @description, @type
	
	-- Begin Return Select <- do not remove
	SELECT [stateId], [description], [type]
	FROM   [dbo].[stateMail]
	WHERE  [stateId] = @stateId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_stateMailUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_stateMailUpdate] 
    @stateId char(10),
    @description char(512),
    @type char(255)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[stateMail]
	SET    [stateId] = @stateId, [description] = @description, [type] = @type
	WHERE  [stateId] = @stateId
	
	-- Begin Return Select <- do not remove
	SELECT [stateId], [description], [type]
	FROM   [dbo].[stateMail]
	WHERE  [stateId] = @stateId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_stateMailDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_stateMailDelete] 
    @stateId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[stateMail]
	WHERE  [stateId] = @stateId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_userDataSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_userDataSelect] 
    @userId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [userId], [apellido], [direccion], [name], [sexo], [telefono] 
	FROM   [dbo].[userData] 
	WHERE  ([userId] = @userId OR @userId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_userDataInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_userDataInsert] 
    @userId char(10),
    @apellido char(30),
    @direccion char(255),
    @name char(255),
    @sexo char(10),
    @telefono char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[userData] ([userId], [apellido], [direccion], [name], [sexo], [telefono])
	SELECT @userId, @apellido, @direccion, @name, @sexo, @telefono
	
	-- Begin Return Select <- do not remove
	SELECT [userId], [apellido], [direccion], [name], [sexo], [telefono]
	FROM   [dbo].[userData]
	WHERE  [userId] = @userId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_userDataUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_userDataUpdate] 
    @userId char(10),
    @apellido char(30),
    @direccion char(255),
    @name char(255),
    @sexo char(10),
    @telefono char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[userData]
	SET    [userId] = @userId, [apellido] = @apellido, [direccion] = @direccion, [name] = @name, [sexo] = @sexo, [telefono] = @telefono
	WHERE  [userId] = @userId
	
	-- Begin Return Select <- do not remove
	SELECT [userId], [apellido], [direccion], [name], [sexo], [telefono]
	FROM   [dbo].[userData]
	WHERE  [userId] = @userId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_userDataDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_userDataDelete] 
    @userId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[userData]
	WHERE  [userId] = @userId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailEnviadosSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_MailEnviadosSelect] 
    @mailEnviadosId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [mailEnviadosId], [casillaCorreoEnviado], [casillaCorreoRecibido], [mailId] 
	FROM   [dbo].[MailEnviados] 
	WHERE  ([mailEnviadosId] = @mailEnviadosId OR @mailEnviadosId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailEnviadosInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_MailEnviadosInsert] 
    @mailEnviadosId char(10),
    @casillaCorreoEnviado char(10),
    @casillaCorreoRecibido char(10),
    @mailId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[MailEnviados] ([mailEnviadosId], [casillaCorreoEnviado], [casillaCorreoRecibido], [mailId])
	SELECT @mailEnviadosId, @casillaCorreoEnviado, @casillaCorreoRecibido, @mailId
	
	-- Begin Return Select <- do not remove
	SELECT [mailEnviadosId], [casillaCorreoEnviado], [casillaCorreoRecibido], [mailId]
	FROM   [dbo].[MailEnviados]
	WHERE  [mailEnviadosId] = @mailEnviadosId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailEnviadosUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_MailEnviadosUpdate] 
    @mailEnviadosId char(10),
    @casillaCorreoEnviado char(10),
    @casillaCorreoRecibido char(10),
    @mailId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[MailEnviados]
	SET    [mailEnviadosId] = @mailEnviadosId, [casillaCorreoEnviado] = @casillaCorreoEnviado, [casillaCorreoRecibido] = @casillaCorreoRecibido, [mailId] = @mailId
	WHERE  [mailEnviadosId] = @mailEnviadosId
	
	-- Begin Return Select <- do not remove
	SELECT [mailEnviadosId], [casillaCorreoEnviado], [casillaCorreoRecibido], [mailId]
	FROM   [dbo].[MailEnviados]
	WHERE  [mailEnviadosId] = @mailEnviadosId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailEnviadosDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_MailEnviadosDelete] 
    @mailEnviadosId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[MailEnviados]
	WHERE  [mailEnviadosId] = @mailEnviadosId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROC [dbo].[usp_MailSelect] 
    @UserId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT m.[MailId], m.[CC], m.[createDate], m.[mensajeId], m.[reciveDate], 
			m.[sendDate], m.[stateId], m.[updateDate],cr.[description] as para,
			ce.[description] as de 
	FROM   [dbo].[Mail] m 
		inner join [dbo].[MailEnviados] me on m.mailId = me.mailId  
		inner join [dbo].[CasillaCorreo] cr on cr.casillacorreoid = me.casillacorreorecibido
		inner join [dbo].[CasillaCorreo] ce on ce.casillacorreoid = me.casillacorreoenviado
	WHERE  
		cr.userId = @UserId
	COMMIT

' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_relationshipSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_relationshipSelect] 
    @officeId CHAR(10),
    @relationshipId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [officeId], [relationshipId], [createDate], [description], [updateDate] 
	FROM   [dbo].[relationship] 
	WHERE  ([officeId] = @officeId OR @officeId IS NULL) 
	       AND ([relationshipId] = @relationshipId OR @relationshipId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_relationshipInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_relationshipInsert] 
    @officeId char(10),
    @relationshipId char(10),
    @createDate datetime,
    @description char(512),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[relationship] ([officeId], [relationshipId], [createDate], [description], [updateDate])
	SELECT @officeId, @relationshipId, @createDate, @description, @updateDate
	
	-- Begin Return Select <- do not remove
	SELECT [officeId], [relationshipId], [createDate], [description], [updateDate]
	FROM   [dbo].[relationship]
	WHERE  [officeId] = @officeId
	       AND [relationshipId] = @relationshipId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_relationshipUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_relationshipUpdate] 
    @officeId char(10),
    @relationshipId char(10),
    @createDate datetime,
    @description char(512),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[relationship]
	SET    [officeId] = @officeId, [relationshipId] = @relationshipId, [createDate] = @createDate, [description] = @description, [updateDate] = @updateDate
	WHERE  [officeId] = @officeId
	       AND [relationshipId] = @relationshipId
	
	-- Begin Return Select <- do not remove
	SELECT [officeId], [relationshipId], [createDate], [description], [updateDate]
	FROM   [dbo].[relationship]
	WHERE  [officeId] = @officeId
	       AND [relationshipId] = @relationshipId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_relationshipDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_relationshipDelete] 
    @officeId char(10),
    @relationshipId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[relationship]
	WHERE  [officeId] = @officeId
	       AND [relationshipId] = @relationshipId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_correoOfficeSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_correoOfficeSelect] 
    @casillaCorreoId CHAR(10),
    @officeId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [casillaCorreoId], [officeId] 
	FROM   [dbo].[correoOffice] 
	WHERE  ([casillaCorreoId] = @casillaCorreoId OR @casillaCorreoId IS NULL) 
	       AND ([officeId] = @officeId OR @officeId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_correoOfficeInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_correoOfficeInsert] 
    @casillaCorreoId char(10),
    @officeId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[correoOffice] ([casillaCorreoId], [officeId])
	SELECT @casillaCorreoId, @officeId
	
	-- Begin Return Select <- do not remove
	SELECT [casillaCorreoId], [officeId]
	FROM   [dbo].[correoOffice]
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [officeId] = @officeId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_correoOfficeUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_correoOfficeUpdate] 
    @casillaCorreoId char(10),
    @officeId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[correoOffice]
	SET    [casillaCorreoId] = @casillaCorreoId, [officeId] = @officeId
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [officeId] = @officeId
	
	-- Begin Return Select <- do not remove
	SELECT [casillaCorreoId], [officeId]
	FROM   [dbo].[correoOffice]
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [officeId] = @officeId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_correoOfficeDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_correoOfficeDelete] 
    @casillaCorreoId char(10),
    @officeId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[correoOffice]
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [officeId] = @officeId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_SessionesSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_SessionesSelect] 
    @sessionId CHAR(512)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [sessionId], [casillaCorreoId], [createDate], [description], [officeId] 
	FROM   [dbo].[Sessiones] 
	WHERE  ([sessionId] = @sessionId OR @sessionId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_SessionesInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_SessionesInsert] 
    @sessionId char(512),
    @casillaCorreoId char(10),
    @createDate datetime,
    @description char(512),
    @officeId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[Sessiones] ([sessionId], [casillaCorreoId], [createDate], [description], [officeId])
	SELECT @sessionId, @casillaCorreoId, @createDate, @description, @officeId
	
	-- Begin Return Select <- do not remove
	SELECT [sessionId], [casillaCorreoId], [createDate], [description], [officeId]
	FROM   [dbo].[Sessiones]
	WHERE  [sessionId] = @sessionId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_SessionesUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_SessionesUpdate] 
    @sessionId char(512),
    @casillaCorreoId char(10),
    @createDate datetime,
    @description char(512),
    @officeId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[Sessiones]
	SET    [sessionId] = @sessionId, [casillaCorreoId] = @casillaCorreoId, [createDate] = @createDate, [description] = @description, [officeId] = @officeId
	WHERE  [sessionId] = @sessionId
	
	-- Begin Return Select <- do not remove
	SELECT [sessionId], [casillaCorreoId], [createDate], [description], [officeId]
	FROM   [dbo].[Sessiones]
	WHERE  [sessionId] = @sessionId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_SessionesDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_SessionesDelete] 
    @sessionId char(512)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[Sessiones]
	WHERE  [sessionId] = @sessionId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_MailInsert] 
    @MailId char(10),
    @CC bit,
    @createDate datetime,
    @mensajeId char(10),
    @reciveDate datetime,
    @sendDate datetime,
    @stateId char(10),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[Mail] ([MailId], [CC], [createDate], [mensajeId], [reciveDate], [sendDate], [stateId], [updateDate])
	SELECT @MailId, @CC, @createDate, @mensajeId, @reciveDate, @sendDate, @stateId, @updateDate
	
	-- Begin Return Select <- do not remove
	SELECT [MailId], [CC], [createDate], [mensajeId], [reciveDate], [sendDate], [stateId], [updateDate]
	FROM   [dbo].[Mail]
	WHERE  [MailId] = @MailId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_MailUpdate] 
    @MailId char(10),
    @CC bit,
    @createDate datetime,
    @mensajeId char(10),
    @reciveDate datetime,
    @sendDate datetime,
    @stateId char(10),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[Mail]
	SET    [MailId] = @MailId, [CC] = @CC, [createDate] = @createDate, [mensajeId] = @mensajeId, [reciveDate] = @reciveDate, [sendDate] = @sendDate, [stateId] = @stateId, [updateDate] = @updateDate
	WHERE  [MailId] = @MailId
	
	-- Begin Return Select <- do not remove
	SELECT [MailId], [CC], [createDate], [mensajeId], [reciveDate], [sendDate], [stateId], [updateDate]
	FROM   [dbo].[Mail]
	WHERE  [MailId] = @MailId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_MailDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_MailDelete] 
    @MailId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[Mail]
	WHERE  [MailId] = @MailId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_listaCorreoSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_listaCorreoSelect] 
    @casillaCorreoId CHAR(10),
    @id CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [casillaCorreoId], [id], [createDate], [description], [name], [updateDate] 
	FROM   [dbo].[listaCorreo] 
	WHERE  ([casillaCorreoId] = @casillaCorreoId OR @casillaCorreoId IS NULL) 
	       AND ([id] = @id OR @id IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_listaCorreoInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_listaCorreoInsert] 
    @casillaCorreoId char(10),
    @id char(10),
    @createDate datetime,
    @description char(512),
    @name char(255),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[listaCorreo] ([casillaCorreoId], [id], [createDate], [description], [name], [updateDate])
	SELECT @casillaCorreoId, @id, @createDate, @description, @name, @updateDate
	
	-- Begin Return Select <- do not remove
	SELECT [casillaCorreoId], [id], [createDate], [description], [name], [updateDate]
	FROM   [dbo].[listaCorreo]
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [id] = @id
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_listaCorreoUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_listaCorreoUpdate] 
    @casillaCorreoId char(10),
    @id char(10),
    @createDate datetime,
    @description char(512),
    @name char(255),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[listaCorreo]
	SET    [casillaCorreoId] = @casillaCorreoId, [id] = @id, [createDate] = @createDate, [description] = @description, [name] = @name, [updateDate] = @updateDate
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [id] = @id
	
	-- Begin Return Select <- do not remove
	SELECT [casillaCorreoId], [id], [createDate], [description], [name], [updateDate]
	FROM   [dbo].[listaCorreo]
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [id] = @id	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_listaCorreoDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_listaCorreoDelete] 
    @casillaCorreoId char(10),
    @id char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[listaCorreo]
	WHERE  [casillaCorreoId] = @casillaCorreoId
	       AND [id] = @id

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_LogSelect] 
    @date DATETIME,
    @description CHAR(512),
    @type CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [date], [description], [type], [casillaCorreoId] 
	FROM   [dbo].[Log] 
	WHERE  ([date] = @date OR @date IS NULL) 
	       AND ([description] = @description OR @description IS NULL) 
	       AND ([type] = @type OR @type IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_LogInsert] 
    @date datetime,
    @description char(512),
    @type char(10),
    @casillaCorreoId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[Log] ([date], [description], [type], [casillaCorreoId])
	SELECT @date, @description, @type, @casillaCorreoId
	
	-- Begin Return Select <- do not remove
	SELECT [date], [description], [type], [casillaCorreoId]
	FROM   [dbo].[Log]
	WHERE  [date] = @date
	       AND [description] = @description
	       AND [type] = @type
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_LogUpdate] 
    @date datetime,
    @description char(512),
    @type char(10),
    @casillaCorreoId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[Log]
	SET    [date] = @date, [description] = @description, [type] = @type, [casillaCorreoId] = @casillaCorreoId
	WHERE  [date] = @date
	       AND [description] = @description
	       AND [type] = @type
	
	-- Begin Return Select <- do not remove
	SELECT [date], [description], [type], [casillaCorreoId]
	FROM   [dbo].[Log]
	WHERE  [date] = @date
	       AND [description] = @description
	       AND [type] = @type	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_LogDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_LogDelete] 
    @date datetime,
    @description char(512),
    @type char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[Log]
	WHERE  [date] = @date
	       AND [description] = @description
	       AND [type] = @type

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_casillaCorreoSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_casillaCorreoSelect] 
    @casillaCorreoId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [casillaCorreoId], [createDate], [description], [password], [updateDate], [userId] 
	FROM   [dbo].[casillaCorreo] 
	WHERE  ([casillaCorreoId] = @casillaCorreoId OR @casillaCorreoId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_casillaCorreoInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_casillaCorreoInsert] 
    @casillaCorreoId char(10),
    @createDate datetime,
    @description char(512),
    @password char(255),
    @updateDate datetime,
    @userId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[casillaCorreo] ([casillaCorreoId], [createDate], [description], [password], [updateDate], [userId])
	SELECT @casillaCorreoId, @createDate, @description, @password, @updateDate, @userId
	
	-- Begin Return Select <- do not remove
	SELECT [casillaCorreoId], [createDate], [description], [password], [updateDate], [userId]
	FROM   [dbo].[casillaCorreo]
	WHERE  [casillaCorreoId] = @casillaCorreoId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_casillaCorreoUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_casillaCorreoUpdate] 
    @casillaCorreoId char(10),
    @createDate datetime,
    @description char(512),
    @password char(255),
    @updateDate datetime,
    @userId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[casillaCorreo]
	SET    [casillaCorreoId] = @casillaCorreoId, [createDate] = @createDate, [description] = @description, [password] = @password, [updateDate] = @updateDate, [userId] = @userId
	WHERE  [casillaCorreoId] = @casillaCorreoId
	
	-- Begin Return Select <- do not remove
	SELECT [casillaCorreoId], [createDate], [description], [password], [updateDate], [userId]
	FROM   [dbo].[casillaCorreo]
	WHERE  [casillaCorreoId] = @casillaCorreoId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_casillaCorreoDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_casillaCorreoDelete] 
    @casillaCorreoId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[casillaCorreo]
	WHERE  [casillaCorreoId] = @casillaCorreoId

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_officeSelect]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_officeSelect] 
    @officeId CHAR(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  

	BEGIN TRAN

	SELECT [officeId], [createDate], [description], [name], [relationshipId], [updateDate] 
	FROM   [dbo].[office] 
	WHERE  ([officeId] = @officeId OR @officeId IS NULL) 

	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_officeInsert]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_officeInsert] 
    @officeId char(10),
    @createDate datetime,
    @description char(512),
    @name char(255),
    @relationshipId char(10),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN
	
	INSERT INTO [dbo].[office] ([officeId], [createDate], [description], [name], [relationshipId], [updateDate])
	SELECT @officeId, @createDate, @description, @name, @relationshipId, @updateDate
	
	-- Begin Return Select <- do not remove
	SELECT [officeId], [createDate], [description], [name], [relationshipId], [updateDate]
	FROM   [dbo].[office]
	WHERE  [officeId] = @officeId
	-- End Return Select <- do not remove
               
	COMMIT
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_officeUpdate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_officeUpdate] 
    @officeId char(10),
    @createDate datetime,
    @description char(512),
    @name char(255),
    @relationshipId char(10),
    @updateDate datetime
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	UPDATE [dbo].[office]
	SET    [officeId] = @officeId, [createDate] = @createDate, [description] = @description, [name] = @name, [relationshipId] = @relationshipId, [updateDate] = @updateDate
	WHERE  [officeId] = @officeId
	
	-- Begin Return Select <- do not remove
	SELECT [officeId], [createDate], [description], [name], [relationshipId], [updateDate]
	FROM   [dbo].[office]
	WHERE  [officeId] = @officeId	
	-- End Return Select <- do not remove

	COMMIT TRAN
' 
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_officeDelete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [dbo].[usp_officeDelete] 
    @officeId char(10)
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
	BEGIN TRAN

	DELETE
	FROM   [dbo].[office]
	WHERE  [officeId] = @officeId

	COMMIT
' 
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Mail_casillaCorreo]') AND parent_object_id = OBJECT_ID(N'[dbo].[Mail]'))
ALTER TABLE [dbo].[Mail]  WITH CHECK ADD  CONSTRAINT [FK_Mail_casillaCorreo] FOREIGN KEY([stateId])
REFERENCES [dbo].[casillaCorreo] ([casillaCorreoId])
GO
ALTER TABLE [dbo].[Mail] CHECK CONSTRAINT [FK_Mail_casillaCorreo]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Mail_mensaje]') AND parent_object_id = OBJECT_ID(N'[dbo].[Mail]'))
ALTER TABLE [dbo].[Mail]  WITH CHECK ADD  CONSTRAINT [FK_Mail_mensaje] FOREIGN KEY([mensajeId])
REFERENCES [dbo].[mensaje] ([mensajeID])
GO
ALTER TABLE [dbo].[Mail] CHECK CONSTRAINT [FK_Mail_mensaje]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Mail_stateMail]') AND parent_object_id = OBJECT_ID(N'[dbo].[Mail]'))
ALTER TABLE [dbo].[Mail]  WITH CHECK ADD  CONSTRAINT [FK_Mail_stateMail] FOREIGN KEY([MailId])
REFERENCES [dbo].[stateMail] ([stateId])
GO
ALTER TABLE [dbo].[Mail] CHECK CONSTRAINT [FK_Mail_stateMail]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_casillaCorreo_userData]') AND parent_object_id = OBJECT_ID(N'[dbo].[casillaCorreo]'))
ALTER TABLE [dbo].[casillaCorreo]  WITH CHECK ADD  CONSTRAINT [FK_casillaCorreo_userData] FOREIGN KEY([userId])
REFERENCES [dbo].[userData] ([userId])
GO
ALTER TABLE [dbo].[casillaCorreo] CHECK CONSTRAINT [FK_casillaCorreo_userData]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_MailEnviados_casillaCorreo]') AND parent_object_id = OBJECT_ID(N'[dbo].[MailEnviados]'))
ALTER TABLE [dbo].[MailEnviados]  WITH CHECK ADD  CONSTRAINT [FK_MailEnviados_casillaCorreo] FOREIGN KEY([casillaCorreoEnviado])
REFERENCES [dbo].[casillaCorreo] ([casillaCorreoId])
GO
ALTER TABLE [dbo].[MailEnviados] CHECK CONSTRAINT [FK_MailEnviados_casillaCorreo]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_MailEnviados_casillaCorreo1]') AND parent_object_id = OBJECT_ID(N'[dbo].[MailEnviados]'))
ALTER TABLE [dbo].[MailEnviados]  WITH CHECK ADD  CONSTRAINT [FK_MailEnviados_casillaCorreo1] FOREIGN KEY([casillaCorreoRecibido])
REFERENCES [dbo].[casillaCorreo] ([casillaCorreoId])
GO
ALTER TABLE [dbo].[MailEnviados] CHECK CONSTRAINT [FK_MailEnviados_casillaCorreo1]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_MailEnviados_Mail]') AND parent_object_id = OBJECT_ID(N'[dbo].[MailEnviados]'))
ALTER TABLE [dbo].[MailEnviados]  WITH CHECK ADD  CONSTRAINT [FK_MailEnviados_Mail] FOREIGN KEY([mailId])
REFERENCES [dbo].[Mail] ([MailId])
GO
ALTER TABLE [dbo].[MailEnviados] CHECK CONSTRAINT [FK_MailEnviados_Mail]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_correoOffice_casillaCorreo]') AND parent_object_id = OBJECT_ID(N'[dbo].[correoOffice]'))
ALTER TABLE [dbo].[correoOffice]  WITH CHECK ADD  CONSTRAINT [FK_correoOffice_casillaCorreo] FOREIGN KEY([casillaCorreoId])
REFERENCES [dbo].[casillaCorreo] ([casillaCorreoId])
GO
ALTER TABLE [dbo].[correoOffice] CHECK CONSTRAINT [FK_correoOffice_casillaCorreo]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_correoOffice_office]') AND parent_object_id = OBJECT_ID(N'[dbo].[correoOffice]'))
ALTER TABLE [dbo].[correoOffice]  WITH CHECK ADD  CONSTRAINT [FK_correoOffice_office] FOREIGN KEY([officeId])
REFERENCES [dbo].[office] ([officeId])
GO
ALTER TABLE [dbo].[correoOffice] CHECK CONSTRAINT [FK_correoOffice_office]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Sessiones_casillaCorreo]') AND parent_object_id = OBJECT_ID(N'[dbo].[Sessiones]'))
ALTER TABLE [dbo].[Sessiones]  WITH CHECK ADD  CONSTRAINT [FK_Sessiones_casillaCorreo] FOREIGN KEY([casillaCorreoId])
REFERENCES [dbo].[casillaCorreo] ([casillaCorreoId])
GO
ALTER TABLE [dbo].[Sessiones] CHECK CONSTRAINT [FK_Sessiones_casillaCorreo]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Sessiones_office]') AND parent_object_id = OBJECT_ID(N'[dbo].[Sessiones]'))
ALTER TABLE [dbo].[Sessiones]  WITH CHECK ADD  CONSTRAINT [FK_Sessiones_office] FOREIGN KEY([officeId])
REFERENCES [dbo].[office] ([officeId])
GO
ALTER TABLE [dbo].[Sessiones] CHECK CONSTRAINT [FK_Sessiones_office]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_listaCorreo_casillaCorreo]') AND parent_object_id = OBJECT_ID(N'[dbo].[listaCorreo]'))
ALTER TABLE [dbo].[listaCorreo]  WITH CHECK ADD  CONSTRAINT [FK_listaCorreo_casillaCorreo] FOREIGN KEY([casillaCorreoId])
REFERENCES [dbo].[casillaCorreo] ([casillaCorreoId])
GO
ALTER TABLE [dbo].[listaCorreo] CHECK CONSTRAINT [FK_listaCorreo_casillaCorreo]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Log_casillaCorreo]') AND parent_object_id = OBJECT_ID(N'[dbo].[Log]'))
ALTER TABLE [dbo].[Log]  WITH CHECK ADD  CONSTRAINT [FK_Log_casillaCorreo] FOREIGN KEY([casillaCorreoId])
REFERENCES [dbo].[casillaCorreo] ([casillaCorreoId])
GO
ALTER TABLE [dbo].[Log] CHECK CONSTRAINT [FK_Log_casillaCorreo]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Log_LogType]') AND parent_object_id = OBJECT_ID(N'[dbo].[Log]'))
ALTER TABLE [dbo].[Log]  WITH CHECK ADD  CONSTRAINT [FK_Log_LogType] FOREIGN KEY([type])
REFERENCES [dbo].[LogType] ([type])
GO
ALTER TABLE [dbo].[Log] CHECK CONSTRAINT [FK_Log_LogType]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_relationship_office1]') AND parent_object_id = OBJECT_ID(N'[dbo].[relationship]'))
ALTER TABLE [dbo].[relationship]  WITH CHECK ADD  CONSTRAINT [FK_relationship_office1] FOREIGN KEY([officeId])
REFERENCES [dbo].[office] ([officeId])
GO
ALTER TABLE [dbo].[relationship] CHECK CONSTRAINT [FK_relationship_office1]
